namespace SubhadraSolutions.Sharp.Logging
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;

    using SubhadraSolutions.Sharp.Logging.Contracts;
    using SubhadraSolutions.Sharp.Logging.Loggers;
    using SubhadraSolutions.Sharp.Utils;
    using SubhadraSolutions.Sharp.Utils.Contracts;

    public sealed class LoggerLogPublisherFactory : AbstractInitializable
    {
        #region Fields

        private static readonly LoggerLogPublisherFactory _instance =
            new LoggerLogPublisherFactory();

        private readonly object _syncLock = new object();

        private ILogger _defaultLogger;

        #endregion Fields

        #region Constructors

        private LoggerLogPublisherFactory()
        {
            UseDefaultIfLoggerDoesNotExist = true;
        }

        #endregion Constructors

        #region Properties

        public static LoggerLogPublisherFactory Instance
        {
            get { return _instance; }
        }

        public List<IUnique> Components
        {
            get;
            set;
        }

        public ILogger DefaultLogger
        {
            get
            {
                Initialize();
                return _defaultLogger;
            }
        }

        public string DefaultLoggerName
        {
            get;
            set;
        }
        public List<ILogSubscriber> LogSubscribers
        {
            get;
            set;
        }

        public bool UseDefaultIfLoggerDoesNotExist
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        public ILogger GetLogger(string loggerId)
        {
            var logger = getComponent<ILogger>(loggerId);
            if (logger == null && UseDefaultIfLoggerDoesNotExist)
            {
                return _defaultLogger;
            }
            return logger;
        }

        public ILogPublisher GetLogPublisher(string logPublisherId)
        {
            var logPublisher = getComponent<ILogPublisher>(logPublisherId);
            return logPublisher;
        }

        protected override void InitializeProtected()
        {
            if (Components != null)
            {
                for (int i = 0; i < Components.Count; i++)
                {
                    if (Components[i] == null)
                    {
                        Components.RemoveAt(i);
                        i--;
                        continue;
                    }
                    try
                    {
                        initializeComponent(Components[i]);
                        if (Components[i] is ILogger &&
                            Components[i].Name == DefaultLoggerName)
                        {
                            _defaultLogger = Components[i] as ILogger;
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }
                }
            }

            if (_defaultLogger == null)
            {
                var logger = new ConsoleDebugTraceLogger();
                _defaultLogger = logger;
            }
            if (LogSubscribers != null)
            {
                foreach (ILogSubscriber subscriber in LogSubscribers)
                {
                    initializeComponent(subscriber);
                }
            }
        }

        private T getComponent<T>(string componentName)
            where T : IUnique
        {
            //Initialize();
            if (!string.IsNullOrWhiteSpace(componentName))
            {
                if (Components != null)
                {
                    foreach (IUnique v in Components)
                    {
                        if (v is T && v.Name == componentName)
                        {
                            return (T)v;
                        }
                    }
                }
            }
            return default(T);
        }

        private void initializeComponent(object o)
        {
            var initializable = o as IInitializable;
            if (initializable != null && !initializable.IsInitialized)
            {
                initializable.Initialize();
            }
        }

        #endregion Methods
    }
}